# -*- coding: utf8 -*-
'''
    1.4.2 图像导数
'''

from PIL import Image
from pylab import array, figure, gray, subplot, imshow, axis, show, zeros, np
from scipy.ndimage import filters


def imx(im, sigma):
    imgx = zeros(im.shape)
    filters.gaussian_filter(im, sigma, (0, 1), imgx)
    return imgx


def imy(im, sigma):
    imgy = zeros(im.shape)
    filters.gaussian_filter(im, sigma, (1, 0), imgy)
    return imgy


def mag(im, sigma):
    imgmag = 255 - np.sqrt(imgx ** 2 + imgy ** 2)
    return imgmag


infile = 'img/haizei.jpg'
im = array(Image.open(infile).convert('L'))

figure()
gray()

# 标准差
sigma = [2, 5, 10]

for i in sigma:
    subplot(3, 4, 4*(sigma.index(i))+1)
    axis('off')
    imshow(im)

    imgx = imx(im, i)
    subplot(3, 4, 4*(sigma.index(i))+2)
    axis('off')
    imshow(imgx)

    imgy = imy(im, i)
    subplot(3, 4, 4*(sigma.index(i))+3)
    axis('off')
    imshow(imgy)

    imgmag = mag(im, i)
    subplot(3, 4, 4*(sigma.index(i))+4)
    axis('off')
    imshow(imgmag)

show()
